*********************************************************************
** Hou et al. SMJ 2023: To construct main text, figures and tables **
*********************************************************************

clear all
set more off

global stataout [for user to define]
global outfile [for user to define]


*********************************
** Table 1. Summary statistics **
*********************************

use   "strt_cont.dta", clear 
global ctrvar emp_ln revt_per_inh ppe_per_inh   
global sample if manuf == 1
ppmlhdfe npat CAFC $ctrvar  $sample , ///
    absorb(year pd_ct) cluster(circuit gvkey)  d
keep if e(sample)
replace fragip_pd_wt = fragip_pd_wt * 1000 
drop if year >= 1983 & year <= 1985
preserve 
use "CAFC_ct_updated.dta", clear
sum ratepre_all_wt
global mean_pre = r(mean)
sum ratepost_all_wt
global mean_post = r(mean)
restore
gen CAFC_wt = ratepre_all_wt 
replace CAFC_wt =  ratepost_all_wt if year >= 1986
replace CAFC = CAFC_wt

global sample if manuf == 1
ppmlhdfe npat CAFC $ctrvar  $sample , ///
    absorb(year pd_ct) cluster(circuit gvkey)  d

keep if e(sample)
*****panel a
label var npat "All patent applications"
label var strt_yes "Strategic patents"
label var inde_pat "Conventional patents"
label var fragip_pd_wt "Fragmentation (1,000)"
label var entry_sum "New entry"
label var ncites_pd_ct_rate "Local competition"
iebaltab npat strt_yes inde_pat  ///
    entry_sum fragip_pd_wt  ncites_pd_ct_rate,  ///
  grpvar(post) save("$stataout\sumtab_ct.xlsx") ///
  grplabels(0 Pre-CAFC @ 1 Post-CAFC) format(%9.5f) rowvarlabels replace 
*****panel b
gen rd_per = xrd1/emp_ipolated
keep gvkey year post emp_ipolated revt_per ppe_per   xrd1
duplicates drop 
label var emp_ipolated "Employees"
label var revt_per "Revenue per employee (real)"
label var ppe_per "PPE per employee (real)"
label var xrd1 "R&D expenditure (real)"
iebaltab  emp_ipolated revt_per ppe_per xrd1 ,  ///
  grpvar(post) save("$stataout\sumtab_cmp") ///
  grplabels(0 Pre-CAFC @ 1 Post-CAFC) format(%9.3f) rowvarlabels replace 


********************************************************
** Figure 1. Conventional vis-a-vis strategic patents **
********************************************************

use "CAFC_ct_updated.dta", clear
gen gap = ratepost_all - ratepre_all 
sum gap, de 
gen high_gap = 1 if gap >= r(p50)
replace high_gap = 0 if mi(high_gap)
gen group = 1 if gap_all <  .0944426
replace group = 2 if gap_all >=  .0944426 &  gap_all < .6011077 
replace group = 3 if gap_all >= .6011077 & gap_all < .9918184  
replace group = 4 if gap_all >= .9918184  
keep circuit high_gap  gap group
tempfile temp
save "'temp'"
use   "strt_cont.dta", clear  
//keep $sample 
merge m:1 circuit using "'temp'"
keep if _merge == 3
drop _merge 

gcollapse (sum) npat_sum = npat inde_sum = inde_pat con_sum = strt_yes  ///
 (mean) npat_mean = npat inde_mean = inde_pat con_mean = strt_yes , ///
 by(high_gap year)
bysort year (high_gap): gen inde_gap = inde_mean - inde_mean[_n-1]
bysort year (high_gap): gen strt_gap = con_mean - con_mean[_n-1]

twoway  (scatter inde_mean year if high_gap == 1  , ///
  lcolor("178 24 43" ) mcolor("178 24 43" ) msymbol(D) connect(direct) lwidth(medthick)) ///
 (scatter  con_mean year if high_gap == 1  ,  ///
 lcolor("178 24 43" ) mcolor("178 24 43" ) msymbol(X) connect(direct) lwidth(medthick) yaxis(2) ) ///
  (scatter inde_gap year if high_gap == 1  , ///
  lcolor(  gs10 ) mcolor(  gs10 ) msymbol(D) connect(direct) lwidth(medthick)) ///
  (scatter  inde_mean year if high_gap == 0  ,  ///
 lcolor("33 102 172") mcolor("33 102 172") msymbol(D)  connect(direct) lwidth(medthick)) ///
  (scatter  con_mean year if high_gap == 0 ,  ///
 lcolor("33 102 172") mcolor("33 102 172" ) msymbol(X)  connect(direct) lwidth(medthick) yaxis(2) )  ///
  (scatter  strt_gap year if high_gap == 1  ,  ///
 lcolor(gs10) mcolor(gs10) msymbol(X) connect(direct) lwidth(medthick)  yaxis(2) )  , ///
   xlab(1976(3)1992) xline(1983 1986)  legend(pos(6)) ///
  legend(col(2) order( 1 "Conventional: above-median shift"  4 "Strategic: above-median shift" ///
   3 "Conventional: below-median shift" 5 "Strategic: below-median shift"    ///
   2 "Conventional: difference" 6 "Strategic: difference" ) colgap(*2) ) ///
  ytitle("Conventional patent applications") ytitle( "Strategic patent applications", axis(2) ) xtitle("Year")  
graph export "$graph\strtindpat_meangap.png", as(png) replace 


****************************
** Figure 2. Placebo test **
****************************

preserve 
use "CAFC_ct_updated.dta", clear
sum gap_all, de
gen high_increase = cond(gap_all >= `r(p50)', 1, 0)
keep circuit high_increase
tempfile ct 
save "'ct'", replace 
restore
use "strt_cont.dta", clear
merge m:1 circuit using "CAFC_ct_updated.dta", nogen
global ctrvar emp_ln revt_per_inh ppe_per_inh    
 forvalues i = 1976/1992{
 gen CAFC_`i' = ratepre_all  
 replace CAFC_`i' = ratepost_all if year == `i'
}
tsset, clear 
merge m:1 circuit using "'ct'"

*****(a) conventional patents
xi: bootstrap, cluster(circuit gvkey) seed(20200805) reps(1000):  ///
ppmlhdfe inde_pat CAFC_1976-CAFC_1981 CAFC_1983-CAFC_1992 $ctrvar ///
 if   manuf == 1,   absorb(pd_ct year) cluster(circuit gvkey) 
coefplot , vertical keep(CAFC_* )  ///
  recast(rcap) ciopts(lwidth(medthick) recast(rcap)) xline(6.5) yline(0) ///
   xlabel( 1 "1976"   4 "1979"  ///
   7 "1983"   10 "1986"   13 "1989" 16 "1992")  
graph export "$graph\CAFC_indepatpretrend_1982.png", as(png) replace

*****(b) Strategic patents
xi: bootstrap, cluster(circuit gvkey) seed(20200805) reps(1000):  ///
ppmlhdfe strt_yes CAFC_1976-CAFC_1981 CAFC_1983-CAFC_1992 $ctrvar ///
 if  manuf == 1,   absorb(pd_ct year) cluster(circuit gvkey) 
**# Bookmark #1
coefplot , vertical keep(CAFC_* )  ///
  recast(rcap) ciopts(lwidth(medthick) recast(rcap)) xline(6.5) yline(0) ///
   xlabel( 1 "1976"   4 "1979"  ///
   7 "1983"   10 "1986"   13 "1989" 16 "1992")  
graph export "$graph\CAFC_strtpatpretrend_1982.png", as(png) replace


***************************************************
** Table 2. CAFC: Effects on patenting and entry **
***************************************************

use "strt_cont.dta", clear
global ctrvar emp_ln revt_per_inh ppe_per_inh   
global sample if manuf == 1
drop if year >= 1983 & year <= 1985

capture program drop pat_reg
program define pat_reg
 local chi2:  display %6.2f  `e(chi2)' 
 gen sample = e(sample)
 unique gvkey if sample == 1
 local cmp: display %6.0f `r(unique)'
 unique pdpass if sample == 1 
 local pd: display %6.0f `r(unique)'
 
 lincom CAFC*($mean_post - $mean_pre)
 local est = r(estimate)
 local psave =  r(p) 
 local pvalue: display %6.3f `psave'
 local change = (exp(`est') - 1)*100
 local perct: display %6.1f `change'
 local obs: display %6.0f `e(N)'
 local r2: display %6.2f `r(r2_a)'
 drop sample
 
outreg, $outopt  merge(table) ///
 ctitle( "VARIABLES", "`1'"  ) ///
 addrows( "P value for CAFC", "`pvalue'" \ "Business-circuit FE", "Yes" ///
  \ "Year FE", "Yes" \  ///
  "Businesses", "`pd'" \ "Companies", "`cmp'" \  ///
  "Observation", "`obs'" \  "Adjusted R-squared", "`r2'"  \ ///
   "Chi-squared", "`chi2'"    \   "CAFC economic effect", "`perct'%" )
end


preserve 
use "CAFC_ct_updated.dta", clear
sum ratepre_all_wt
global mean_pre = r(mean)
sum ratepost_all_wt
global mean_post = r(mean)
restore

global sample if manuf == 1
ppmlhdfe npat CAFC $ctrvar  $sample , ///
    absorb(year pd_ct) cluster(circuit gvkey)  d

keep if e(sample)


outreg, clear(table)
xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe npat CAFC $ctrvar  , a(pd_ct year)
pat_reg "(a) All patents"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe inde_pat CAFC $ctrvar, a(pd_ct year )
pat_reg "(b) conventional"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe strt_yes CAFC $ctrvar, a(pd_ct year)
pat_reg "(c) Strategic"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe inde_pat CAFC $ctrvar_wrd , a(pd_ct  year)
pat_reg "(d) Conventional: strt_controlling for R&D"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe strt_yes CAFC $ctrvar_wrd , a(pd_ct year)
pat_reg "(e) Strategic: strt_controlling for R&D"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe entry_sum CAFC $ctrvar , a(pd_ct  year)
pat_reg "(f) Entry: strt_controlling for R&D"

cd "$stataout"
outreg using "$outfile.doc", $outopt ///
 replay(table) replace title("Main results") ///
 note("$note") 
 

************************************************* 
** Table 3. Strategic patenting: Contingencies **
************************************************* 

outreg, clear(table)
foreach i in complex high_fragippd  high_ppgt_int high_ncites_pd_ct_rate  ///
  high_emp_ipolated    {

outreg, clear(table)
 
xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe inde_pat CAFC $ctrvar if manuf == 1 & `i' == 1 , a(pd_ct year)
pat_reg "High `i' (a) Conventional"
 
xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe inde_pat CAFC $ctrvar if manuf == 1 & `i' == 0 , a(pd_ct year)
pat_reg "Low `i' (b)"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe strt_yes CAFC $ctrvar if manuf == 1 & `i' ==1  , a(pd_ct year)
pat_reg "High `i' (c) Strategic"

xi: bootstrap, cluster(circuit gvkey) seed($seed) reps($reps): ///
ppmlhdfe strt_yes CAFC $ctrvar if manuf == 1 &  `i' == 0 , a(pd_ct year)
pat_reg "Low `i' (d)"
  
cd "$stataout"
outreg using "$outfile.doc", $outopt ///
 replay(table) addt title("CAFC and patents (by `i')") ///
 note("$note") 
}

 
 

